package cn.ekuma.pos.toolcommand.base;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.gui.muleSelecter.JMuleObjectSelecter;
import com.openbravo.data.gui.muleSelecter.MuleComboBoxValEntry;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.format.Formats;
import cn.ekuma.epos.datalogic.I_DataLogicERP;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JComboBox;

public class SelectTableDialog extends JDialog implements ItemListener {

	private final JPanel contentPanel;
	private JTextField jTxtStartDate;
	private JTextField jTxtEndDate;
	I_DataLogicERP dataLogic;
	private JMuleObjectSelecter selecter;
	private JComboBox cbSelectTableType;

	/**
	 * Create the dialog.
	 */
	public SelectTableDialog(I_DataLogicERP dataLogic) {
		this.dataLogic=dataLogic;
		setBounds(100, 100, 600, 450);
		getContentPane().setLayout(new BorderLayout());
		contentPanel = new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		selecter=new JMuleObjectSelecter();
		contentPanel.add(selecter, BorderLayout.CENTER);
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
			{
				JLabel lblNewLabel = new JLabel("时间启");
				panel.add(lblNewLabel);
			}
			{
				jTxtStartDate = new JTextField();
				panel.add(jTxtStartDate);
				jTxtStartDate.setColumns(10);
			}
			{
				JButton btnTimeStart = new JButton("");
				btnTimeStart.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						btnDateStartActionPerformed(e);
					}
				});
				btnTimeStart.setIcon(new ImageIcon(SelectTableDialog.class.getResource("/com/openbravo/images/date.png")));
				panel.add(btnTimeStart);
			}
			{
				JLabel lblNewLabel_1 = new JLabel("时间止");
				panel.add(lblNewLabel_1);
			}
			{
				jTxtEndDate = new JTextField();
				panel.add(jTxtEndDate);
				jTxtEndDate.setColumns(10);
			}
			{
				JButton btnTimeEnd = new JButton("");
				btnTimeEnd.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						btnDateEndActionPerformed(e);
					}
				});
				btnTimeEnd.setIcon(new ImageIcon(SelectTableDialog.class.getResource("/com/openbravo/images/date.png")));
				panel.add(btnTimeEnd);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("执行导出");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						doJob();
					}
				});
				{
					cbSelectTableType = new JComboBox();
					cbSelectTableType.setMaximumRowCount(20);
					buttonPane.add(cbSelectTableType);
					cbSelectTableType.addItemListener(this);
				}
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("退出");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						SelectTableDialog.this.dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}

		cbSelectTableType.addItem(new ComboBoxValEntry(I_DataLogicERP.TABLE_Base,"基本资料库"));
		cbSelectTableType.addItem(new ComboBoxValEntry(I_DataLogicERP.TABLE_Crm,"CRM库"));
		cbSelectTableType.addItem(new ComboBoxValEntry(I_DataLogicERP.TABLE_Erp,"业务库"));
		cbSelectTableType.addItem(new ComboBoxValEntry(I_DataLogicERP.TABLE_All,"全部库"));
		//initList();
		
	}

	
	private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateStartActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateStartActionPerformed

	private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btnDateEndActionPerformed
		Date date;
		try {
			date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e) {
			date = null;
		}
		date = JCalendarDialog.showCalendarTimeHours(this, date);
		if (date != null) {
			jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
		}
	}// GEN-LAST:event_btnDateEndActionPerformed

	private void initList(int loadtype) {
	   List<TableDefinition> defTables=dataLogic.getImpExpTableList(loadtype);
	   ArrayList<MuleComboBoxValEntry> entrys=new ArrayList<MuleComboBoxValEntry>();
	   for(TableDefinition table:defTables)
		   entrys.add(new MuleComboBoxValEntry(table,table.getTableName()));
	   selecter.setEntrys(entrys);
	}


	protected void doJob() {
		List<MuleComboBoxValEntry> entrys=selecter.getSelectedList();
		ArrayList<TableDefinition> exportTables=new ArrayList<TableDefinition>();
		for(MuleComboBoxValEntry e:entrys)
			exportTables.add((TableDefinition) e.getKey());
		Date startdate;
		try {
			startdate = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
		} catch (BasicException e1) {
			startdate=null;
			e1.printStackTrace();
		}
		Date enddate;
		try {
			enddate = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
		} catch (BasicException e1) {
			enddate=null;
			e1.printStackTrace();
		}
		try {
			dataLogic.doExportTableDateDb(exportTables, startdate, enddate);
			new MessageInf(MessageInf.SGN_SUCCESS,"执行成功").show(this);
		} catch (BasicException e1) {
			new MessageInf(e1).show(this);
			e1.printStackTrace();
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		ComboBoxValEntry entry=(ComboBoxValEntry) cbSelectTableType.getSelectedItem();
		initList((Integer) entry.getKey());
	}

}
